package com.spit.fam;

import android.app.Application;
import android.util.Log;
import android.widget.Toast;

import com.gg.reader.api.dal.GClient;
import com.gg.reader.api.protocol.gx.MsgAppGetBaseVersion;
import com.gg.reader.api.protocol.gx.MsgBaseStop;
import com.spit.fam.keyctrl.GlobalClient;
import com.spit.fam.keyctrl.PowerUtil;

/**
 * User: Nike
 * 2025/1/17 13:05
 */
public class MyApplication extends Application {

    @Override
    public void onCreate() {
        super.onCreate();
        PowerUtil.power("1");

        int[] baudRateList = {460800, 115200};
        for (int i : baudRateList) {
            boolean b = openReader(String.valueOf(i));
            if (b) {
                break;
            }
        }
    }

    private GClient client = GlobalClient.getClient();
    private boolean rfidError;

    private boolean openReader(String baudRate) {
        try {
            if (client.openAndroidSerial("/dev/ttyS3:" + baudRate, 0)) {
                rfidError = true;
                MsgBaseStop stop = new MsgBaseStop();
                client.sendSynMsg(stop);
                if (stop.getRtCode() == 0) {
                    MsgAppGetBaseVersion getBaseVersion = new MsgAppGetBaseVersion();
                    client.sendSynMsg(getBaseVersion);
                    Log.e("RFID模块连接成功", getBaseVersion.getBaseVersions());
                    return true;
                } else {
                    client.close();
                    return false;
                }
            } else {
                Toast.makeText(this, "識別串口失敗，請重新打開APP", Toast.LENGTH_SHORT).show();
            }
        }catch (Exception e){
            Log.e("Exception", e.toString());
        }
        return false;
    }
}
